<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局  公司信息 -->
        <treeTools :tree-node="company" :tree-root="true" @addDepartment="addDepartment" />
        <hr>
        <el-tree :data="depts" :props="defaultProps" default-expand-all>
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <template #default="obj">
            <treeTools
              :tree-node="obj.data"
              @delDepartments="getDepartment"
              @addDepartment="addDepartment"
              @editDepartment="editDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <!-- 接收弹窗组件的显示与隐藏 -->
      <addDept
        ref="addDept"
        :is-show-dialog.sync="isShowDialog"
        :tree-node="treeNode"
        @addDepartment="getDepartment"
        @closeDialog="isShowDialog=false"
      />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartment } from '@/api/department'
import { listToreeData } from '@/utils'
import addDept from './components/add-dept.vue'// 引入封装的弹窗
export default {
  components: { treeTools,
    addDept },
  data() {
    return {
      isShowDialog: false,
      treeNode: {},
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const { depts } = await getDepartment()
      // 调用封装的组织架构传一个空的id
      this.depts = listToreeData(depts, '')
      console.log(depts)
    },
    // 子组件传过来的addDepartment，上面用@接收，点击之后让弹窗组件的isShowDialog变成true
    addDepartment(treeNode) {
      this.isShowDialog = true
      this.treeNode = treeNode
    },
    // 编辑框回显操作
    editDepartment(treeNode) {
      this.$refs.addDept.getDepartmentB(treeNode.id)
      // c传递数据
      this.treeNode = treeNode
      this.isShowDialog = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
